package com.bigfreakindeals.Protocols.SoapHandlers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.content.Intent;
import android.util.Log;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Models.ActiveDeal;
import com.bigfreakindeals.Models.BeansSays;
import com.bigfreakindeals.Models.PayPalObject;
import com.bigfreakindeals.Models.UserProfile;

public class PayPalWebHandler  {
	
	static Boolean isLogin;
	
	static final String RESPONSE = "response";
	static final String STATUS = "status";
	static final String PAYPAL_REQ_FORM = "paypal_request_form";
	
	static final String ID = "id";
	static final String METHOD = "method";
	static final String ACTION = "action";
	static final String INPUTS = "inputs";
	static final String ITEM = "item";
	
	static final String ITEM_TYPE = "type";
	static final String ITEM_NAME = "name";
	static final String ITEM_VALUE = "value";
	
		static final  String ERROR = "error";
	
	UserProfile userProfile = new UserProfile();
	String feedUrl;
	 
    private static final String SOAP_ACTION = "1";
    private static final String METHOD_NAME = "bigfreakindeals";
    private static final String NAMESPACE = "1";
    
    
    
    
	public PayPalWebHandler(String feedUrl) {
	    	this.feedUrl = feedUrl;
	    	System.out.println("paypalwebhander:"+feedUrl);
	    	parse();
	}
	
	 public void parse() {

	     try {
		 
	    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.setOutputSoapObject(request);
		
		    HttpTransportSE androidHttpTransport = new HttpTransportSE(feedUrl);

			androidHttpTransport.call(SOAP_ACTION, envelope);
			
			SoapObject response =(SoapObject)envelope.getResponse();
			boolean isValid =false;;
		
		 	System.out.println("step1");
			   
			String status  = response.getProperty(STATUS).toString();
	       		 if (status !=null && status.length() >0){
	       			 if (status.contentEquals("false")){
	       				isValid = false;
	       			 }else{
	       				isValid = true;
	       			 }
	       	}
	       		 

	 		System.out.println("step2"+isValid);
	 		
	       	if(isValid){
	       		SoapObject paypal_request_form  = (SoapObject)response.getProperty("paypal_request_form");
		       	
	       		Vector inputs = (Vector)paypal_request_form.getProperty("inputs");
	    		
	    		System.out.println(" items "+inputs.size());
	    		PayPalObject paypalObj = new PayPalObject();
	    		String name; String value;
	    		for (int count = 0;count <= inputs.size() -1; count++){
					
					SoapObject item = (SoapObject)inputs.get(count);
					
					if (item.getProperty("name") !=null && (item.getProperty("value")!=null)){
						System.out.println(" name"+item.getProperty("name").toString());
						name = item.getProperty("name").toString().trim();
						value = item.getProperty("value").toString().trim();
						System.out.println(" name"+name+" - "+value+" ");
						
						if (name.endsWith("currency_code")){
							paypalObj.currencycode = value;
					    }
						if (name.endsWith("amount")){
							paypalObj.amount = 	value;
					    }
						if (name.endsWith("business")){
							paypalObj.recipient = 	value;
					    }
						if (name.endsWith("item_name")){
							paypalObj.itemname = 	value;
					    }
						if (name.endsWith("os0")){
							paypalObj.os0 = value;
					    }
						if (name.endsWith("os1")){
							paypalObj.os1 = value;
					    }
						
						if (name.endsWith("os2")){
							paypalObj.os2 = value;
					    }
						
						if (name.endsWith("os3")){
							paypalObj.os3 = value;
					 
						}
						if (name.endsWith("on0")){
							paypalObj.on0 = value;
					    }
						if (name.endsWith("on1")){
							paypalObj.on1 = value;
					    }
						
						if (name.endsWith("on2")){
							paypalObj.on2 = value;
					    }
						
						if (name.endsWith("on3")){
							paypalObj.on3 = value;
					 
						}
						
						if (name.endsWith("notify_url")){
							paypalObj.url_notify = 	value;
				 		}
						if (name.endsWith("return")){
							paypalObj.url_return = 	value;
				 		}
						if (name.endsWith("cancel_return")){
							paypalObj.url_cancel_return = 	value;
				 		}
						
						if (name.endsWith("paymentaction")){
							paypalObj.paymentaction = value;
				 		}
						if (name.endsWith("no_note")){
							paypalObj.no_note = value;
				 		}
						if (name.endsWith("no_shipping")){
							paypalObj.no_shipping = value;
				 		}
						
					}
			    	

					
					
				}//end for
	    		
	    		paypalObj.setMemo();
	    		
	    		ApplicationEx.getInstance().myProfile.paypalObj = null;
	    		
	    		ApplicationEx.getInstance().myProfile.paypalObj = paypalObj;
	    		
	    		Intent intent = new Intent(Constant.EVENTS_GETPAYPAL_IPN_SUCCESS); 
	    		ApplicationEx.getInstance().sendBroadcast(intent);
					
			}else{//no beans says avail
				//String error = userObj.getProperty(ERROR).toString();
				String errormessage = "There is an error processing your paypal request. Please try again later";
				Intent intent = new Intent(Constant.EVENTS_GETPAYPAL_IPN_ERROR); 
				intent.putExtra(Constant.TAG_WEBSERVICE_ERROR_MESSAGE, errormessage);
				ApplicationEx.getInstance().sendBroadcast(intent);
				
			}
			
	     } catch (IOException e1) {
				// TODO Auto-generated catch block
	    		
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
		
			} catch (Exception e1) {
				
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
	     
		
		
	 }
		
}